package com.controller;

import com.domain.Travel;
import com.domain.User;
import com.service.TravelService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
     private TravelService travelService;

    @RequestMapping("/login")
    public ModelAndView userLogin(User user,HttpSession httpSession){
        System.out.println(user);
        user=userService.userLogin(user);
        System.out.println(user);
        List<Travel> travels=travelService.findAllTravel();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("travels",travels);
        if (user!=null){
            modelAndView.setViewName("navi");
            httpSession.setAttribute("userid",user.getUserid());
            httpSession.setAttribute("username",user.getUsername());
            httpSession.setAttribute("password",user.getPassword());
            httpSession.setAttribute("userage",user.getUserage());
            httpSession.setAttribute("usersex",user.getUsersex());
        } else {
            modelAndView.setViewName("error");
        }
        return modelAndView;
    }

    @RequestMapping("/regist")
    public ModelAndView userRegister(User user){
        System.out.println(user);
        boolean r=userService.userRegister(user);
        ModelAndView modelAndView= new ModelAndView();
        List<Travel> travels=travelService.findAllTravel();
        modelAndView.addObject("travels",travels);
        if (r==true){
            modelAndView.setViewName("navi");
        }else {
            modelAndView.setViewName("error");
        }
        return modelAndView;
    }

    @RequestMapping("/registPage")
    public String toRegistPage(){
        return "register";
    }

    @RequestMapping("/loginPage")
    public String toLoginPage(){
        return "login";
    }

    @RequestMapping("/toMyComment")
    public ModelAndView toMyComment(Integer travelid){
        ModelAndView modelAndView= new ModelAndView();
        modelAndView.addObject("travelid",travelid);
        modelAndView.setViewName("myComment");
        return modelAndView;
    }

    @RequestMapping("/toMyTravel")
    public String toMyTravel(){
        return "myTravel";
    }

    @RequestMapping("/toMyQuestion")
    public String toMyQuestion(){
        return "myQuestion";
    }

    @RequestMapping("/userInfo")
    public ModelAndView userInfo(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("userInfo");
        return modelAndView;
    }



}
